class Solution {
public:
    int minSpeedOnTime(vector<int>& dist, double hour) {
        int left = 1, right = 1e7, ans = INT_MAX;
        while (left <= right) {
            int mid = (left + right) / 2;
            double total = 0;
            for (int i = 0; i < dist.size() - 1; i++) {
                total += (dist[i] + mid - 1) / mid;
            }
            total += (double)dist.back() / (double)mid;
            if (total <= hour) {
                ans = min(mid, ans);
                right = mid - 1;
            }
            else {
                left = mid + 1;
            }
        }
        return ans == INT_MAX ? -1 : ans;
    }
};